package com.talent.jiwen_teacher.my;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.talent.jiwen_teacher.R;
import com.talent.jiwen_teacher.base.BaseActivity;
import com.talent.jiwen_teacher.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen_teacher.http.common.ApiFactory;
import com.talent.jiwen_teacher.http.common.HttpUtil;
import com.talent.jiwen_teacher.http.common.ProgressSubscriber;
import com.talent.jiwen_teacher.http.result.PriceResult;
import com.talent.jiwen_teacher.util.SPUtil;
import com.talent.jiwen_teacher.util.Validators;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;


/**
 * Created by qyqx on 2018/9/25.
 * 定价中心
 */

public class PricingActivity extends BaseActivity {


    @BindView(R.id.txt_money)
    TextView txt_money;

    @BindView(R.id.edit_money)
    EditText edit_money;


    String moneyStr;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_price;
    }

    @Override
    public String titleName() {
        return "定价申请";
    }

    @Override
    protected void initData() {

        getTeacherPrice();
    }

    @OnClick({R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.submit:
                moneyStr = edit_money.getText().toString().trim();
                if (!Validators.isEmpty(moneyStr)) {
                    try {
                        double money = Double.parseDouble(moneyStr);
                        teacherUpdateCharge(money);
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                } else {
                    showToast("请填写提价金额");
                }
                break;
        }
    }


    /**
     * 保存定价信息
     */
    private void teacherUpdateCharge(double money) {
        Map<String, String> map = new HashMap<>();
        map.put("chargeAmount", "" + money);

        Observable observable = ApiFactory.getEducationApiSingleton().teacherUpdateCharge(SPUtil.getToken(), map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<Void>(PricingActivity.this) {
            @Override
            protected void _onNext(Void v) {
                showToast("提交成功，请等待审核");
                finish();
            }
            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject, true);

    }


    /**获取地址信息
     *
     */
    private void getTeacherPrice(){
        Observable observable = ApiFactory.getEducationApiSingleton().getTeacherPrice(SPUtil.getToken());
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<PriceResult>(PricingActivity.this) {
            @Override
            protected void _onNext(PriceResult priceResult) {
                if(priceResult!=null && !Validators.isEmpty(priceResult.getTeachPrice())){
                    txt_money.setText(priceResult.getTeachPrice());
                }else{
                    txt_money.setText("2.0");
                }

            }

            @Override
            protected void _onError(String message) {
                showToast(message);

            }
        } , ActivityLifeCycleEvent.DESTROY , lifecycleSubject , false);

    }
}
